package com.zwj.batch.api.controller;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zwj.batch.core.batch.job.BatchJob;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 张文接
 * @version 1.0
 * @createTime 2024/3/6 16:50
 */
@RestController
@RequestMapping("/batch")
public class BatchController {
    /**
     * Job 业务处理类
     */
    @Resource
    private BatchJob batchJob;
    /**
     * SpringBatch 启动器
     */
    @Resource
    private JobLauncher jobLauncher;
    /**
     * springBatch 参数构造器
     */
    @Resource
    private JobParametersBuilder jobParametersBuilder;

    /**
     * 测试案例 1
     *
     * @return 执行结果
     */
    @XxlJob("test1")
    public ReturnT<String> test1() {
        //构架springBatch 参数
        jobParametersBuilder.addString("name", "hello springBatch");
        JobParameters jobParameters = jobParametersBuilder.toJobParameters();
        try {
            //启动批处理
            jobLauncher.run(batchJob.test1Job(), jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ReturnT.SUCCESS;
    }
}
